package org.multi.core.util;

import java.io.BufferedReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.multi.core.CharMulti;
import org.multi.core.Multi;

public class FileUtils {

    public static Multi<String> parse(String path) {
        return Multi.create(consumer -> {
            try (BufferedReader reader = Files.newBufferedReader(Paths.get(path))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    consumer.accept(line);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
    }

    public static CharMulti parseToChar(String path) {
        return CharMulti.create(consumer -> {
            try (BufferedReader reader = Files.newBufferedReader(Paths.get(path))) {
                int c;
                while ((c = reader.read()) != -1) {
                    consumer.accept((char) c);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
    }
}
